package csu.fun.spring_store.persistence;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import csu.fun.spring_store.domain.Order;
import csu.fun.spring_store.domain.OrderVO;
import net.sf.jsqlparser.expression.JsonAggregateFunction;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Repository
@Mapper
public interface OrderMapper extends BaseMapper<Order> {

    JSONObject profile(@Param("id") String id);
   List<OrderVO> selectOrders();
    void deleteorderinlineitem(@Param("orderid") int orderid);
    List<JSONObject> selectlineitem(@Param("orderid") int orderid);
    void bianjiitem(@Param("orderid") int orderid,@Param("itemid") String itemid,@Param("quantity") int quantity);
    void bianjiorder(Order order);

    @Insert("insert into lineitem (orderid,linenum,itemid,quantity,unitprice) values(#{id},#{ln},#{itemid},#{num},#{decimal})")
    void addline(@Param("id") int id,@Param("ln") int ln,@Param("itemid") String itemid,@Param("num") int num,@Param("decimal") BigDecimal decimal);

    @Delete("delete from lineitem where orderid =#{id} and itemid= #{itemid} ")
    void deleteordernum(@Param("id") int id,@Param("itemid")String itemid);
    //判断是否存在指定id的order（orderid不区分大小写）
    default boolean isExistOrder(int orderid) {
        QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("orderid", orderid);
        return this.exists(queryWrapper);
    }

    //判断指定用户是否有订单记录
    default boolean isUserHasOrder(String userid) {
        return this.getOrdersListByUserid(userid).size() > 0;
    }

    //返回指定orderid的order
    default Order getOrderByOrderid(int orderid) {
        return this.selectById(orderid);
    }

    //返回指定用户的所有order
    default List<Order> getOrdersListByUserid(String userid) {
        QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userid", userid);
        return this.selectList(queryWrapper);
    }

    /*
      单独修改数据库中的某个值，返回修改的行数。orderid必须已经在数据库中存在，否则不进行任何修改
     */
    default int setUseridByOrderid(int orderid, String userid) {
        Order order = this.selectById(orderid);
        if (order != null) {
            order.setUserid(userid);
            return this.updateById(order);
        }
        return 0;
    }
    default int setOrderDateByOrderid(int orderid, Date orderDate) {
        Order order = this.selectById(orderid);
        if (order != null) {
            order.setOrderDate(orderDate);
            return this.updateById(order);
        }
        return 0;
    }
    default int setShipAddr1ByOrderid(int orderid, String shipAddr1) {
        Order order = this.selectById(orderid);
        if (order != null) {
            order.setShipAddr1(shipAddr1);
            return this.updateById(order);
        }
        return 0;
    }
    default int setShipAddr2ByOrderid(int orderid, String shipAddr2) {
        Order order = this.selectById(orderid);
        if (order != null) {
            order.setShipAddr2(shipAddr2);
            return this.updateById(order);
        }
        return 0;
    }
    default int setShipCityByOrderid(int orderid, String shipCity) {
        Order order = this.selectById(orderid);
        if (order != null) {
            order.setShipCity(shipCity);
            return this.updateById(order);
        }
        return 0;
    }
    default int setShipStateByOrderid(int orderid, String shipState) {
        Order order = this.selectById(orderid);
        if (order != null) {
            order.setShipState(shipState);
            return this.updateById(order);
        }
        return 0;
    }
    default int setShipZipByOrderid(int orderid, String shipZip) {
        Order order = this.selectById(orderid);
        if (order != null) {
            order.setShipZip(shipZip);
            return this.updateById(order);
        }
        return 0;
    }
    default int setShipCountryByOrderid(int orderid, String shipCountry) {
        Order order = this.selectById(orderid);
        if (order != null) {
            order.setShipCountry(shipCountry);
            return this.updateById(order);
        }
        return 0;
    }
    default int setBillAddr1ByOrderid(int orderid, String billAddr1) {
        Order order = this.selectById(orderid);
        if (order != null) {
            order.setBillAddr1(billAddr1);
            return this.updateById(order);
        }
        return 0;
    }
    default int setBillAddr2ByOrderid(int orderid, String billAddr2) {
        Order order = this.selectById(orderid);
        if (order != null) {
            order.setBillAddr2(billAddr2);
            return this.updateById(order);
        }
        return 0;
    }
    default int setBillCityByOrderid(int orderid, String billCity) {
        Order order = this.selectById(orderid);
        if (order != null) {
            order.setBillCity(billCity);
            return this.updateById(order);
        }
        return 0;
    }
    default int setBillStateByOrderid(int orderid, String billState) {
        Order order = this.selectById(orderid);
        if (order != null) {
            order.setBillState(billState);
            return this.updateById(order);
        }
        return 0;
    }
    default int setBillZipByOrderid(int orderid, String billZip) {
        Order order = this.selectById(orderid);
        if (order != null) {
            order.setBillZip(billZip);
            return this.updateById(order);
        }
        return 0;
    }
    default int setBillCountryByOrderid(int orderid, String billCountry) {
        Order order = this.selectById(orderid);
        if (order != null) {
            order.setBillCountry(billCountry);
            return this.updateById(order);
        }
        return 0;
    }
    default int setCourierByOrderid(int orderid, String courier) {
        Order order = this.selectById(orderid);
        if (order != null) {
            order.setCourier(courier);
            return this.updateById(order);
        }
        return 0;
    }
    default int setTotalPriceByOrderid(int orderid, double totalPrice) {
        Order order = this.selectById(orderid);
        if (order != null) {
            BigDecimal bigDecimal = new BigDecimal(totalPrice);
            order.setTotalPrice(bigDecimal);
            return this.updateById(order);
        }
        return 0;
    }
    default int setShipToFirstnameByOrderid(int orderid, String shipToFirstname) {
        Order order = this.selectById(orderid);
        if (order != null) {
            order.setShipToFirstname(shipToFirstname);
            return this.updateById(order);
        }
        return 0;
    }
    default int setShipToLastnameByOrderid(int orderid, String shipToLastname) {
        Order order = this.selectById(orderid);
        if (order != null) {
            order.setShipToLastname(shipToLastname);
            return this.updateById(order);
        }
        return 0;
    }
    default int setBillToFirstnameByOrderid(int orderid, String billToFirstname) {
        Order order = this.selectById(orderid);
        if (order != null) {
            order.setBillToFirstname(billToFirstname);
            return this.updateById(order);
        }
        return 0;
    }
    default int setBillToLastnameByOrderid(int orderid, String billToLastname) {
        Order order = this.selectById(orderid);
        if (order != null) {
            order.setBillToLastname(billToLastname);
            return this.updateById(order);
        }
        return 0;
    }
    default int setCreditCardByOrderid(int orderid, String creditCard) {
        Order order = this.selectById(orderid);
        if (order != null) {
            order.setCreditCard(creditCard);
            return this.updateById(order);
        }
        return 0;
    }
    default int setExprDateByOrderid(int orderid, String exprDate) {
        Order order = this.selectById(orderid);
        if (order != null) {
            order.setExprDate(exprDate);
            return this.updateById(order);
        }
        return 0;
    }
    default int setCardTypeByOrderid(int orderid, String cardType) {
        Order order = this.selectById(orderid);
        if (order != null) {
            order.setCardType(cardType);
            return this.updateById(order);
        }
        return 0;
    }
    default int setLocaleByOrderid(int orderid, String locale) {
        Order order = this.selectById(orderid);
        if (order != null) {
            order.setLocale(locale);
            return this.updateById(order);
        }
        return 0;
    }

    //删除指定id的product，返回实际修改的行数
    default int removeOrderByOrderid(int orderid) {
        return this.deleteById(orderid);
    }

    //删除指定用户的所有order，返回实际删除的行数
    default int removeAllOrdersByUserid(String userid) {
        QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userid", userid);
        return this.delete(queryWrapper);
    }

}
